##########################################################################################

library(dplyr)
library(ggplot2)
library(data.table)
library(RColorBrewer)
library(optparse)
library(ggpubr)
library(ggsci)

##########################################################################################

option_list <- list(
    make_option(c("--input_file"), type = "character") ,
    make_option(c("--ccf_file"), type = "character") ,
    make_option(c("--images_path"), type = "character")
)

if(1!=1){
    
    work_dir <- "~/20220915_gastric_multiple/dna_combine/"
    input_file <- paste(work_dir,"/baseTable/STAD_Info.addBurden.tsv",sep="")
    ccf_file <- paste(work_dir,"/mutationTime/result/All_CCF_mutTime.tsv",sep="")
	images_path <- paste(work_dir,"/images/mutBurden",sep="")

}

###########################################################################################

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

input_file <- opt$input_file
ccf_file <- opt$ccf_file
images_path <- opt$images_path

dir.create(images_path , recursive = T)

###########################################################################################

col <- c(
  brewer.pal(9,"YlGnBu")[6],
  rgb(234,106,79,alpha=255,maxColorValue=255),
  rgb(203,24,30,alpha=255,maxColorValue=255),
  rgb(255,0,0,alpha=255,maxColorValue=255)
  )

names(col) <- c("IM" , "IGC" , "DGC" , "GC")
col <- col[1:3]

Variant_Types <- c("Missense_Mutation","Nonsense_Mutation","Frame_Shift_Ins","Frame_Shift_Del","In_Frame_Ins","In_Frame_Del","Splice_Site","Nonstop_Mutation")

###########################################################################################

dat_sample <- data.frame(fread( input_file ))
dat_ccf <- data.frame(fread( ccf_file ))
dat_info <- data.frame(Tumor = dat_sample$Tumor , Patient = dat_sample$Patient)

###########################################################################################
## 判断样本是否发生TP53突变
dat_ccf <- subset( dat_ccf , Hugo_Symbol=="TP53" & Variant_Classification %in% Variant_Types )
dat_sample$TP53 <- ifelse( dat_sample$Tumor %in% dat_ccf$Sample , "Mut" , "NoMut"  )

###########################################################################################
## 判断样本是否发生TP53的recurrent突变
dat_ccf <- merge( dat_ccf , dat_info , by.x = "Sample" , by.y = "Tumor" )
dat_ccf$vid <- paste( dat_ccf$Chr , dat_ccf$Start_Position , dat_ccf$REF , dat_ccf$ALT , sep = ":" )
dat_recurrent <- dat_ccf %>%
group_by( vid ) %>%
summarize( mutNum = length(unique(Patient)) )
dat_recurrent <- dat_recurrent[dat_recurrent$mutNum > 1,]
recurrentSample <- dat_ccf[dat_ccf$vid %in% dat_recurrent$vid,"Sample"]

dat_sample$TP53 <- ifelse( dat_sample$Tumor %in% recurrentSample , "ReMut" , dat_sample$TP53   )

###########################################################################################
## 多个样本负荷取中位数
dat_plot2 <- dat_sample %>%
group_by( Patient , Class , Type , TP53 ) %>%
summarize( BurdenAll = median(BurdenAll) ,BurdenExon = median(BurdenExon) )

dat_plot2$Class <- factor( dat_plot2$Class , levels = names(col) , order = T )
dat_plot2$TP53 <- factor( dat_plot2$TP53 , levels = c("ReMut" , "Mut" , "NoMut") , order = T )

###########################################################################################

plotBurden <- function(dat_plot_tmp = dat_plot_tmp , mutType = mutType){

    my_comparisons_1 <- list(c(1, 2) , c(1,3) , c(2,3))

    plot <- ggplot( dat_plot_tmp , aes( x = TP53 , y = MutBurden_use , color = TP53 ) ) +
        geom_boxplot(alpha =1 , outlier.size=0 , size = 1.5 , width = 0.6) +
        geom_jitter(position = position_jitter(0.17) , size = 1 , alpha = 0.7) +
        facet_grid(.~Class,space='free_x',scales='free_x') +
        scale_color_npg() +
        xlab(NULL) +
        ylab("Mutation rate per MB")+
        theme_bw() +
        stat_compare_means(comparisons = my_comparisons_1) +
        theme(panel.background = element_blank(),#设置背影为白色#清除网格线
            legend.position ='left',
            legend.title = element_blank() ,
            panel.grid.major=element_line(colour=NA),
            plot.title = element_text(size = 8,color="black",face='bold'),
            legend.text = element_text(size = 10,color="black",face='bold'),
            axis.text.y = element_text(size = 10,color="black",face='bold'),
            axis.title.x = element_text(size = 10,color="black",face='bold'),
            axis.title.y = element_text(size = 10,color="black",face='bold'),
            axis.ticks.x = element_blank(),
            axis.text.x = element_text(size = 10,color="black",face='bold') ,
            axis.line = element_line(size = 0.5)) 
    
    out_name <- paste0( images_path , "/mutBurden.TP53." , mutType , ".recurrent.pdf" )  
    ggsave(file=out_name,plot=plot,width=7,height=5)

}

dat_plot_tmp <- dat_plot2

for(mutType in c("cds" , "all")){

    if(mutType=="cds"){
        dat_plot_tmp$MutBurden_use <- dat_plot_tmp$BurdenExon
        plotBurden(dat_plot_tmp = dat_plot_tmp , mutType = mutType)
    }else if(mutType=="all"){
        dat_plot_tmp$MutBurden_use <- dat_plot_tmp$BurdenAll
        plotBurden(dat_plot_tmp = dat_plot_tmp , mutType = mutType)
    }
}
